package command;

import dao.LoginDao;
import util.ConfigEnum;
import util.ConfigurationManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 07.10.13
 * Time: 16:25
 * To change this template use File | Settings | File Templates.
 */
public class RegCommand implements Command {
    LoginDao dao = new LoginDao();

    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
        //     System.out.println("start action");
        String login = request.getParameter("login");
        String password = request.getParameter("password");
        String mail = request.getParameter("mail");
        String name = request.getParameter("name");
        String page;

        if (dao.inserUser(login, password, name, mail)) {
            if (dao.checkUser(login, password)) {
                request.setAttribute("user", login);
                page = ConfigurationManager.getInstance().getProperty(ConfigEnum.MAIN_PAGE_PATH);

            } else {
                page = ConfigurationManager.getInstance().getProperty(ConfigEnum.ERROR_PAGE_PATH);
            }


        } else {
            page = ConfigurationManager.getInstance().getProperty(ConfigEnum.ERROR_PAGE_PATH);
        }


       return page;

    }

}
